package org.elanfox.intellicommunity.msg.dto;

import lombok.Data;
import org.elanfox.intellicommunity.msg.ienum.CmdEnums;

import java.io.Serializable;

/**
 * 报文基类
 *
 * @author allan
 * @version 1.0.0
 * @date 2020/1/6
 */
@Data
public class RawMsgDTO implements Serializable {
    private static final long serialVersionUID = 530520256271384225L;

    /**
     * 命令代码
     */
    private CmdEnums.Code cmdCode;
    /**
     * 设备地址原始16进制
     */
    private String rawHexAddr;
    /**
     * 设备地址
     */
    private int deviceAddr;

    public RawMsgDTO(CmdEnums.Code cmdCode, String hexStr) {
        this.cmdCode = cmdCode;
        this.rawHexAddr = hexStr.substring(28, 32);
//        this.deviceAddr = Integer.parseInt(this.rawHexAddr.substring(2, 4), 16);
        this.deviceAddr = Integer.parseInt(this.rawHexAddr, 16);

//        this.protocol = ByteBufUtil.hexDump(rawMsg, 6, 2);
//        // TODO: 2020/1/6 length
//        String hexCmdCode = ByteBufUtil.hexDump(rawMsg, 13, 1);
//        System.out.println(hexCmdCode);
//        this.cmdCode = SingleArgEnumHelper.getEnumTypeByValue(CmdCodeEnum.class, hexCmdCode);
//
//        if (this.cmdCode == CmdCodeEnum.UPLOAD_TAG && rawMsg.readableBytes() > 19) {
//            //消息体
//            String hexTagType = ByteBufUtil.hexDump(rawMsg, 19, 1);
//            this.tagType = SingleArgEnumHelper.getEnumTypeByValue(TagTypEnum.class, hexTagType);
//            this.tagId = ByteBufUtil.hexDump(rawMsg, 20, 4);
//            this.newTriggerId = ByteBufUtil.hexDump(rawMsg, 25, 2);
//            this.oldTriggerId = ByteBufUtil.hexDump(rawMsg, 27, 2);
//            String binaryStatus = Integer.toBinaryString(Integer.parseInt(ByteBufUtil.hexDump(rawMsg, 33, 1)));
//            this.batteryFulled = binaryStatus.charAt(0) == '0';
//            this.alarmedButton = binaryStatus.charAt(1) == '1';
//            this.dismantleAlarm = binaryStatus.charAt(2) == '1';
//            long timestamp = Long.valueOf(ByteBufUtil.hexDump(rawMsg, 35, 4), 16);
//            this.time = Jsr310Utils.DateTime.of(timestamp * 1000);
//        }
    }
}
